import useContentSwapFormStore from '@renderer/store/Form/ContentSwapFormStore'

export default function useContentSwapTransfer(
  callback: () => ITableDataItem[]
) {
  const contentSwapTransfer = () => {
    // 分割符
    const { splitCharaterText } = useContentSwapFormStore()

    if (splitCharaterText) {
      callback().forEach((item) => {
        const { name } = item.newest
        const splitArr = name.split(splitCharaterText)
        // 大于 1 个元素,代表分割成功
        if (splitArr.length > 1) {
          // 相邻交换位置
          for (let i = 0; i < splitArr.length - 1; i++) {
            const item = splitArr[i]
            splitArr[i] = splitArr[i + 1]
            splitArr[i + 1] = item
          }
          // 重新拼接
          item.newest.name = splitArr.join(splitCharaterText)
        }
      })
    }
  }

  return {
    contentSwapTransfer
  }
}
